package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作商户基础信息表实现类 on 2020/2/25.
 */
public class BspsMchntBaseInfoMapperImpl implements BspsMchntBaseInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public MchntBaseInfo selectBspsMchntBaseInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoById");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntBaseInfo;
    }

    @Override
    public MchntBaseInfo selectBspsMchntBaseInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByPrimaryKey");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntBaseInfo;
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByMchntNoOrMchntName(String instId, String mchntNo, String mchntName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByMchntNoOrMchntName");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntBaseInfo.setMchntNo("%" + mchntNo + "%");
        }
        if (null != mchntName && !mchntName.isEmpty()) {
            mchntBaseInfo.setMchntName("%" + mchntName + "%");
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号或者商户名称模糊查询商户基本信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>", instId, mchntNo, mchntName);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByChainMchntNo(String instId, String chainMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByChainMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setChainMchntNo(chainMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据连锁商户号查询商户基本信息失败,instId:<{}>,chainMchntNo:<{}>", instId, chainMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByPlatMchntNo(String instId, String platMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByPlatMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setPlatMchntNo(platMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据平台商户号查询商户基本信息失败,instId:<{}>,platMchntNo:<{}>", instId, platMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoGroupByPlatMchntNo(String instId, String platMchntNo, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoGroupByPlatMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setPlatMchntNo(platMchntNo);
        mchntBaseInfo.setMchntExpandTime(startDate);
        mchntBaseInfo.setRemark5(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据平台商户号查询商户基本信息失败,instId:<{}>,platMchntNo:<{}>", instId, platMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByGroupMchntNo(String instId, String groupMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByGroupMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setGroupMchntNo(groupMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据集团商户号查询商户基本信息失败,instId:<{}>,groupMchntNo:<{}>", instId, groupMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByAgentMchntNo(String instId, String agentMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByAgentMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setAgentMchntNo(agentMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据代理商商户号查询商户基本信息失败,instId:<{}>,agentMchntNo:<{}>", instId, agentMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoGroupByAgentMchntNo(String instId, String agentMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoGroupByAgentMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setAgentMchntNo(agentMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据代理商查询商户基本信息失败,instId:<{}>,managerId:<{}>", instId, agentMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoByManagerId(String instId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByManagerId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        /* 00-非正式商户01-正式商户02-停用商户 03-已注销商户 */
        mchntBaseInfo.setMchntStatus("01");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据拓展经理查询商户基本信息失败,instId:<{}>,managerId:<{}>", instId, managerId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBspsMchntBaseInfoNeedAuthByManagerId(String instId, String managerId, PageInfo pageInfo) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoNeedAuthByManagerId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        /* 00-非正式商户01-正式商户02-停用商户 03-已注销商户 */
        mchntBaseInfo.setMchntStatus("01");
        return dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoGroupByManagerId(String instId, String managerId, String month) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoGroupByManagerId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        if (StringUtil.isEmptyOrNull(month)) {
            mchntBaseInfo.setMchntExpandTime(DateUtil.getCurrentDateTime("yyyy-MM"));
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据拓展经理查询商户基本信息失败,instId:<{}>,managerId:<{}>", instId, managerId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoGroupByManagerIdAndDate(String instId, String managerId, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoGroupByManagerIdAndDate");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        mchntBaseInfo.setMchntExpandTime(startDate);
        mchntBaseInfo.setRemark5(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据拓展经理查询商户基本信息失败,instId:<{}>,managerId:<{}>,startDate:<{}>,endDate:<{}>", instId, managerId, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBspsMchntBaseInfoBySignInstId(String instId, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoBySignInstId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setSignInstId(signInstId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户签约机构查询商户基本信息失败,instId:<{}>,signInstId:<{}>", instId, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBspsMchntBaseInfoByManagerIdAndDate(String instId, String managerId, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBaseInfoByManagerIdAndDate");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        mchntBaseInfo.setMchntExpandTime(startDate);
        mchntBaseInfo.setRemark5(endDate);
        return dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
    }

}
